package org.springblade.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.app.entity.AppInfoEntity;
import org.springblade.app.entity.AppUserEntity;
import org.springblade.app.mapper.AppUserMapper;
import org.springblade.app.service.IAppUserService;
import org.springblade.app.vo.AppUserVO;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 应用成员表 服务实现类
 *
 * @author Chill
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUserEntity> implements IAppUserService {

	@Override
	public List<AppUserVO> list(String appId, int linkType) {
		if (linkType == 1){
			return baseMapper.listUser(appId);
		}
		if (linkType == 2){
			return baseMapper.listDept(appId);
		}
		try {
			throw new Exception("关联类型异常，请检查linkType是否正确！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}

	@Override
	public IPage<AppUserVO> page(IPage<AppUserVO> page, String subAppId, Integer linkType) {
		if (linkType == 1) {
			return baseMapper.pageUser(page, subAppId);
		}else if (linkType == 2){
			return baseMapper.pageDept(page, subAppId);
		}
		return null;
	}

	@Override
	public List<AppInfoEntity> list(List<Long> linkIds,int linkType) {
		if (linkType == 1){
			return baseMapper.listByUsers(linkIds);
		}
		if (linkType == 2){
			return baseMapper.listByDepts(linkIds);
		}
		try {
			throw new Exception("关联类型异常，请检查linkType是否正确！");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ArrayList<>();
	}

	/**
	 * 根据子应用ID与成员ID查询关联信息
	 * @param subAppId
	 * @param linkId
	 * @return
	 */
	@Override
	public AppUserEntity findBySubAppIdAndLinkId(String subAppId, Long linkId) {
		return getOne(Wrappers.<AppUserEntity>lambdaQuery().eq(AppUserEntity::getAppId, subAppId).eq(AppUserEntity::getLinkId, linkId));
	}
}
